﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace CommonLibrary.DataModel.WorkFlow
{
    public class PageObject
    {
        public bool isFirstSetted()
        {
            return PageNum > 0;
        }

        /**
	     * 根据PageNum和pageSize计算当前页第一条记录在总结果集中的位置
	     */
        public int getFirst()
        {
            return ((PageNum - 1) * PageSize);
        }

        public int getLast()
        {
            return PageNum * PageSize + 1;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="orderField">排序字段</param>
        /// <param name="autoCount">是否自动获得总记录</param>
        public PageObject(int pageSize, bool autoCount)
        {
            this.pageSize = pageSize;
            this.autoCount = autoCount;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageNum">当前页数</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="orderField">排序字段</param>
        /// <param name="autoCount">是否自动获得总记录</param>
        public PageObject(int pageNum, int pageSize, bool autoCount)
        {
            this.pageSize = pageSize;
            this.autoCount = autoCount;
            this.pageNum = pageNum;
        }

        //页数
        
        public int PageNum
        {
            get
            {
                return pageNum;
            }
            set
            {
                this.pageNum = value;
            }
        }

        //总页数
        
        public int TotalPage
        {
            get
            {
                return (int)(TotalCount + PageSize - 1) / PageSize;
            }
            set
            {
                this.totalPage = value;
            }
        }

        //每页数目
        
        public int PageSize
        {
            get
            {
                return pageSize <= 0 ? -1 : pageSize;
            }
            set
            {
                this.pageSize = value;
            }
        }

        //总记录数
        
        public long TotalCount
        {
            set
            {
                this.totalCount = value;
            }
            get
            {
                return totalCount < 0 ? 0 : totalCount;
            }
        }

        //order字段
        
        public string OrderField
        {
            set
            {
                this.orderField = value;
            }
            get
            {
                return orderField;
            }
        }

        //count字段
        
        public string CountField
        {
            set
            {
                this.countField = value;
            }
            get
            {
                if (countField == string.Empty || countField == null)
                    return "*";
                return countField;
            }
        }

        //是否自动分页
        
        public bool AutoCount
        {
            set
            {
                this.autoCount = value;
            }
            get
            {
                return autoCount;
            }
        }

        //是否自动分页
        
        public bool GroupFlag
        {
            set
            {
                this.groupFlag = value;
            }
            get
            {
                return groupFlag;
            }
        }

        //查询结果：交错数组
        
        public string[][] ArrayData
        {
            set
            {
                this.arraydata = value;
            }
            get
            {
                return arraydata;
            }
        }

        //查询结果：DataTable
        
        public DataTable TableData
        {
            set
            {
                this.tableData = value;
            }
            get
            {
                return tableData;
            }
        }

        //是否返回DataTable类型的数据
        
        public bool IsResultTable
        {
            set
            {
                this.isResultTable = value;
            }
            get
            {
                return isResultTable;
            }
        }

        private int pageNum = 1;

        private int pageSize = -1;

        private int totalPage;

        private long totalCount;

        private bool autoCount;                 //自动统计总数

        private bool groupFlag = false;             //是否包含group by

        private bool isResultTable = false;

        private string orderField;

        private string countField;

        private string[][] arraydata;

        private DataTable tableData;

    }
}
